package com.pca.headline.dao.impl;

import com.pca.headline.dao.BaseDao;
import com.pca.headline.dao.NewsHeadlineDao;
import com.pca.headline.pojo.NewsHeadline;
import com.pca.headline.pojo.vo.HeadlineDetailVo;
import com.pca.headline.pojo.vo.HeadlinePageVo;
import com.pca.headline.pojo.vo.HeadlineQueryVo;

import java.util.ArrayList;
import java.util.List;

public class NewsHeadlineDaoImpl extends BaseDao implements NewsHeadlineDao {
    @Override
    public List<HeadlinePageVo> findPageList(HeadlineQueryVo headlineQuery) {
        List params = new ArrayList();
        String sql = """
                    select
                    hid,
                    title,
                    type,
                    page_views pageViews,
                    TIMESTAMPDIFF(HOUR, create_time, NOW()) pastHours,
                    publisher
                    from
                    news_headline
                    where
                    is_deleted=0
                """;
        if(headlineQuery.getType() != 0) {
            sql = sql.concat(" and type=? ");
            params.add(headlineQuery.getType());
        }
        if(headlineQuery.getKeyWords() != null && !headlineQuery.getKeyWords().isEmpty()) {
            sql = sql.concat(" and title like ? ");
            params.add("%" + headlineQuery.getKeyWords() + "%");

        }
        sql = sql.concat(" order by pastHours asc , pageViews desc ");
        sql = sql.concat(" limit ?,?"); //1.从哪个位置下一个开始 2.取多少个
        params.add((headlineQuery.getPageNum() - 1) * headlineQuery.getPageSize());
        params.add(headlineQuery.getPageSize());
        return baseQuery(HeadlinePageVo.class, sql, params.toArray());

    }

    @Override
    public int findCount(HeadlineQueryVo headlineQuery) {
        List params = new ArrayList();

        String sql = """
                    select
                    hid,
                    title,
                    type,
                    page_views pageViews,
                    TIMESTAMPDIFF(HOUR, create_time, NOW()) pastHours,
                    publisher
                    from
                    news_headline
                    where
                    is_deleted=0
                """;
        if(headlineQuery.getType() != 0) {
            sql = sql.concat(" and type=?");
            params.add(headlineQuery.getType());
        }
        if(headlineQuery.getKeyWords() != null && !headlineQuery.getKeyWords().isEmpty()) {
            sql = sql.concat(" and title like ?");
            params.add("%" + headlineQuery.getKeyWords() + "%");
        }

        return baseQueryObject(int.class,sql,params.toArray());
    }

    /**
     *      Integer hid;
     *      String title;
     *      String article;
     *      Integer type;
     *      String typeName;
     *      Integer pageViews;
     *      Long pastHours;
     *      Integer publisher;
     *      String author;
     * @param hid
     * @return
     */
    @Override
    public HeadlineDetailVo findHeadlineDetail(int hid) {
        String sql = """
                    select 
                    h.hid,
                    h.title,
                    h.article,
                    h.type,
                    t.tname typeName,
                    h.page_views pageViews,
                    TIMESTAMPDIFF(HOUR, h.create_time, NOW()) pastHours,
                    h.publisher,
                    u.nick_name author
                    
                    from 
                    news_headline h 
                    left join news_type t on h.type=t.tid
                    left join news_user u on h.publisher=u.uid
                    where
                    hid = ?
                """;
        List<HeadlineDetailVo> list = baseQuery(HeadlineDetailVo.class, sql, hid);
        return list != null && list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public int incrPageViews(int hid) {
        String sql = "update news_headline set page_views = page_views + 1 where hid = ?";
        return baseUpdate(sql, hid);
    }

    @Override
    public int addNewsHeadline(NewsHeadline newsHeadline) {
        String sql = "insert into news_headline values(default,?,?,?,?,0,now(),now(),0)";
        return baseUpdate(sql, newsHeadline.getTitle(),newsHeadline.getArticle(),newsHeadline.getType(),newsHeadline.getPublisher());
    }

    @Override
    public NewsHeadline findByHid(int hid) {
        String sql = """
                    select 
                    hid,
                    title,
                    article,
                    type,
                    publisher,
                    page_views pageViews,
                    create_time createTime,
                    update_time updateTime,
                    is_deleted isDeleted
                    from news_headline
                    where hid = ?
                                 
                """;
        List<NewsHeadline> list = baseQuery(NewsHeadline.class, sql, hid);
        return list != null && list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public int update(NewsHeadline headline) {
        String sql = "update news_headline set title = ?, article = ?, type = ?, update_time = now() where hid = ?";
        return baseUpdate(sql, headline.getTitle(),headline.getArticle(),headline.getType(),headline.getHid());
    }

    @Override
    public int removeByHid(int hid) {
        String sql = "update news_headline set is_deleted = 1 where hid = ?";
        return baseUpdate(sql, hid);
    }
}


























